package com.xiaoyu.lib_av.datamodel.message

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.util.StringUtil

class AVRoomMessage {

    val senderUid: String

    @AVRoomMessageType.AVRoomMessageTypeDef
    private var type: String? = AVRoomMessageType.INVALID

    @AVRoomMessageType.AVRoomSystemMessageSubtype.AVRoomSystemMessageSubtypeDef
    private var subtype: String? = AVRoomMessageType.AVRoomSystemMessageSubtype.INVALID

    @AVRoomMessageType.AVRoomMessageActionSubtype.AVRoomMessageActionDef
    private var action: String? = AVRoomMessageType.AVRoomMessageActionSubtype.INVALID
    private var targetUserId: String? = ""
    private var text: String? = ""
    var attrs: JsonData? = null
        private set

    private constructor(senderUid: String) {
        this.senderUid = senderUid
    }

    private constructor(jsonData: JsonData) {
        this.senderUid = jsonData.optString(SENDER_UID)
        this.type = jsonData.optString(TYPE)
        this.subtype = jsonData.optString(SUBTYPE)
        this.action = jsonData.optString(ACTION)
        this.targetUserId = jsonData.optString(TARGET_USER_ID)
        this.text = jsonData.optString(TEXT)
        this.attrs = jsonData.optJson(ATTRS)
    }

    fun getType(): String? {
        return type
    }

    fun setType(type: String?): AVRoomMessage {
        this.type = type
        return this
    }

    fun getSubtype(): String? {
        return subtype
    }

    fun setSubtype(subtype: String?): AVRoomMessage {
        this.subtype = subtype
        return this
    }

    fun putAttrs(attrs: JsonData?): AVRoomMessage {
        if (attrs == null) {
            return this
        }
        this.attrs = attrs
        return this
    }

    fun setAction(@AVRoomMessageType.AVRoomMessageActionSubtype.AVRoomMessageActionDef action: String?): AVRoomMessage {
        this.action = action
        return this
    }

    fun getAction(): String? {
        return action
    }

    fun setTargetUserId(targetUserId: String?): AVRoomMessage {
        this.targetUserId = targetUserId
        return this
    }

    fun getTargetUserId(): String? {
        return targetUserId
    }

    fun getProcessResult(): String? {
        return PROCESS_RESULT
    }

    fun setText(text: String?): AVRoomMessage {
        this.text = text
        return this
    }

    fun getText(): String? {
        return text
    }

    fun toJson(): JsonData {
        val jsonData = JsonData.newMap()
        jsonData.put(SENDER_UID, senderUid)
        jsonData.put(TYPE, type)
        jsonData.put(SUBTYPE, subtype)
        jsonData.put(ACTION, action)
        jsonData.put(TEXT, text)
        jsonData.put(TARGET_USER_ID, targetUserId)
        jsonData.put(ATTRS, attrs)
        return jsonData
    }

    fun toUnSupportedTypeJson(): JsonData {
        val jsonData = JsonData.newMap()
        jsonData.put(SENDER_UID, "")
        jsonData.put(TYPE, type)
        jsonData.put(SUBTYPE, subtype)
        jsonData.put(ACTION, "")
        jsonData.put(TEXT, "【该版本不支持该消息】")
        jsonData.put(TARGET_USER_ID, "")
        jsonData.put(ATTRS, "")
        return jsonData
    }

    override fun toString(): String {
        return "AVRoomMessage{" +
                "senderUid='" + senderUid + '\''.toString() +
                ", targetUserId='" + targetUserId + '\''.toString() +
                ", action='" + action + '\''.toString() +
                ", text='" + text + '\''.toString() +
                ", type='" + type + '\''.toString() +
                ", subtype='" + subtype + '\''.toString() +
                ", attrs=" + attrs +
                '}'.toString()
    }

    fun isInvalid(): Boolean {
        return StringUtil.isAnyEmpty(senderUid, type)
    }

    companion object {

        val INVALID = AVRoomMessage("")

        private val SENDER_UID = "sender_uid"
        private val TYPE = "type"
        private val SUBTYPE = "subtype"
        private val TARGET_USER_ID = "target_user_id"
        private val ACTION = "action"
        private val TEXT = "text"
        public val ATTRS = "attrs"

        val PROCESS_RESULT = "process_result"

        fun from(senderUid: String): AVRoomMessage {
            return AVRoomMessage(senderUid)
        }

        fun from(jsonData: JsonData): AVRoomMessage {
            return AVRoomMessage(jsonData)
        }
    }
}